#!/usr/bin/env groovy
package com.yidao.www

def robotAutoBuild(robot, arch, branch, distro, clean, version){

    def date = new Date()
    def buildVersion
//    if(robot == 'D200'){
    if(robot.contains('D200') ){
        buildVersion = version + '.' + date.format('yyyyMMddHHmm') + '_' + BUILD_NUMBER
    }else{
        buildVersion = version
    }
    def robotBuildProfile = robotBuildProfile(robot, arch, branch, distro, buildVersion)
    def label = robotBuildProfile.label.get(arch).get(distro) + '_' + BUILD_NUMBER
    def image = robotBuildProfile.image.get(arch).get(distro)
    def volumes = robotBuildProfile.volumes.get(arch).get(distro)
    def gitInfo = robotBuildProfile.gitUrl.get(arch)
    def artifactDir = robotBuildProfile.artifactDir
    def artifactName = robotBuildProfile.artifactName
    def artifact =  artifactDir + artifactName
    def buildScript = robotBuildProfile.buildScript
    def cleanScript = robotBuildProfile.cleanScript
    def packageScript = robotBuildProfile.packageScript
    def artifactory = robotBuildProfile.artifactory + '/build'
    def cleanCmd = robotBuildProfile.cleanCmd
    def webhookUrl = robotBuildProfile.webhookUrl
    def base = new base()
    def notification = new notification()
    def jobName = JOB_NAME
    def buildNumber = BUILD_NUMBER
    def jobUrl = JOB_URL
    def logUrl = BUILD_URL + 'console'
    def username = ''
    def deployTarget = base.backupFolder(artifactory, robot, branch, date)
    def server = Artifactory.server 'jfrog-artifactory'
    def srverUrl = server.url
    def storageURL

    println '============================================== BUILD INFO =============================================='
    println robotBuildProfile
    println "label: ${label}"
    println "image: ${image}"
    println "volumes: ${volumes}"
    println "gitInfo: ${gitInfo}"
    println "artifactDir: ${artifactDir}"
    println "artifactName: ${artifactName}"
    println "artifact: ${artifact}"
    println "buildScript: ${buildScript}"
    println "packageScript: ${packageScript}"
    println "artifactory: ${artifactory}"
    println '========================================================================================================'

    podTemplate(
        cloud: 'kubernetes',
        containers: [
            containerTemplate(
                image: image,
                name: 'jnlp',
                ttyEnabled: true,
                alwaysPullImage: true,
                workingDir: '/home/jenkins/agent')
        ],
        label: label,
        name: 'yidao',
        namespace: 'kube-jenkins',
        volumes: volumes) {

            node(label){

                stage('checkout'){
                    storageURL = srverUrl + '/' + deployTarget
                    try{
                        base.gitRepos(gitInfo, branch)
                    }catch(e){
                        println '[ERROR] 代码克隆失败!'
                        println e
                        currentBuild.result = 'FAILURE'
                        base.influxDb()
                        error e.getMessage()
                    }
                }

                stage('build'){
                    if(clean){
                        def status = base.scriptStatus(cleanScript)
                    }
                    try{
                        def status = base.scriptStatus(buildScript)
                        if(status != 0){
                            currentBuild.result = 'FAILURE'
                            base.influxDb()
                            error '[ERROR] 编译失败!'
                        }
                    }catch(e){
                        println '[ERROR] 编译失败!'
                        println e
                        currentBuild.result = 'FAILURE'
                        base.influxDb()
                        notification.wechatNotification(webhookUrl, jobName, buildNumber, jobUrl, logUrl, '', currentBuild.currentResult)
                        error e.getMessage()
                    }
                }

                stage('package'){
                    try{
                        def status = base.scriptStatus(packageScript)
                        if(status != 0){
                            currentBuild.result = 'FAILURE'
                            base.influxDb()
                            error '[ERROR] 打包失败!'
                        }
                    }catch(e){
                        println '[ERROR] 打包失败!'
                        println e
                        currentBuild.result = 'FAILURE'
                        base.influxDb()
                        error e.getMessage()
                    }

                }

                stage('upload'){
                    print "artifact: ${artifact}"
                    print "deployTarget: ${deployTarget}"
                    try{
                        base.fileUpload(artifact, deployTarget)
                    }catch(e){
                        println "[ERROR] 上传失败!"
                        println e
                        currentBuild.result = 'FAILURE'
                        base.influxDb()
                        error e.getMessage()
                    }
                }

                stage('clean'){
                    def status = base.scriptStatus(cleanCmd)
                    if(status != 0){
                        println "[ERROR] 清理失败!"
                    }
                }

                stage('influxDb'){
                    currentBuild.result = 'SUCCESS'
                    base.influxDb()
                }

                stage('retrun artifact'){
                    currentBuild.description = """<span>存储地址:<a href=${storageURL}>${storageURL}</a></span>"""
                    return deployTarget
                }

            }
        }
}


def robotAutoPreBuild(robot, arch, branch, distro, clean, version){

    def date = new Date()
    def buildVersion
    if(robot == 'D200'){
        buildVersion = version + '.' + date.format('yyyyMMddHHmm') + '_' + BUILD_NUMBER
    }else{
        buildVersion = version
    }
    def robotBuildProfile = robotBuildProfile(robot, arch, branch, distro, buildVersion)
    def label = robotBuildProfile.label.get(arch).get(distro) + '_' + BUILD_NUMBER
    def image = robotBuildProfile.image.get(arch).get(distro)
    def volumes = robotBuildProfile.volumes.get(arch).get(distro)
    def gitInfo = robotBuildProfile.gitUrl.get(arch)
    def artifactDir = robotBuildProfile.artifactDir
    def artifactName = robotBuildProfile.artifactName
    def artifact =  artifactDir + artifactName
    def buildScript = robotBuildProfile.buildScript
    def cleanScript = robotBuildProfile.cleanScript
    def packageScript = robotBuildProfile.packageScript
    def artifactory = robotBuildProfile.artifactory + '/preBuild'
    def cleanCmd = robotBuildProfile.cleanCmd
    def base = new base()
    def gitScript = new gitScript()
    def notification = new notification()
    def jobName = JOB_NAME
    def buildNumber = BUILD_NUMBER
    def jobUrl = JOB_URL
    def logUrl = BUILD_URL + 'console'
    def webhookUrl = robotBuildProfile.webhookUrl
    def username = 'changbin'
    def deployTarget = base.backupFolder(artifactory, robot, branch, date)
    def cherryPickDir = robotBuildProfile.cherryPickDir
    def cherryPickGit = robotBuildProfile.cherryPickGit
    def server = Artifactory.server 'jfrog-artifactory'
    def srverUrl = server.url
    def storageURL

    println '============================================== BUILD INFO =============================================='
    println robotBuildProfile
    println "label: ${label}"
    println "image: ${image}"
    println "volumes: ${volumes}"
    println "gitInfo: ${gitInfo}"
    println "artifactDir: ${artifactDir}"
    println "artifactName: ${artifactName}"
    println "artifact: ${artifact}"
    println "buildScript: ${buildScript}"
    println "packageScript: ${packageScript}"
    println "artifactory: ${artifactory}"
    println '========================================================================================================'

    podTemplate(
        cloud: 'kubernetes',
        containers: [
            containerTemplate(
                image: image,
                name: 'jnlp',
                ttyEnabled: true,
                alwaysPullImage: true,
                workingDir: '/home/jenkins/agent')
        ],
        label: label,
        name: 'yidao',
        namespace: 'kube-jenkins',
        volumes: volumes) {

            node(label){

                stage('checkout'){
                    storageURL = srverUrl + '/' + deployTarget
                    try{
                        base.gitRepos(gitInfo, branch)
                    }catch(e){
                        println '[ERROR] 代码克隆失败!'
                        println e
                        currentBuild.result = 'FAILURE'
                        base.influxDb()
                        notification.wechatNotification(webhookUrl, jobName, buildNumber, jobUrl, logUrl, GERRIT_PATCHSET_UPLOADER_NAME, currentBuild.currentResult)
                        error e.getMessage()
                    }
                }

                stage('cherry-pick'){
                    if(clean){
                        def status = base.scriptStatus(cleanScript)
                    }
                    dir(cherryPickDir){
                        try{
                            def status = base.cherryPick(cherryPickGit, GERRIT_REFSPEC)
                            if(status != 0){
                                currentBuild.result = 'FAILURE'
                                base.influxDb()
                                sh "git status"
                                error '[ERROR] cherry-pick 失败!请检查是否有冲突或者已 Merge'
                            }
                            gitScript.checkCommitMessage()
                        }catch(e){
                            println e
                            base.cherryPickFailClean()
                            currentBuild.result = 'FAILURE'
                            base.influxDb()
                            notification.commitTemplateNotification(webhookUrl, jobName, buildNumber, jobUrl, logUrl, GERRIT_PATCHSET_UPLOADER_NAME, e.getMessage())
                            error e.getMessage()
                        }
                    }
                }


                stage('build'){
                    try{
                        def status = base.scriptStatus(buildScript)
                        if(status != 0){
                            currentBuild.result = 'FAILURE'
                            base.influxDb()
                            error '[ERROR] 编译失败!'
                        }
                    }catch(e){
                        println '[ERROR] 编译失败!'
                        println e
                        currentBuild.result = 'FAILURE'
                        dir(cherryPickDir){
                            base.cherryPickClean()
                        }
                        base.influxDb()
                        notification.wechatNotification(webhookUrl, jobName, buildNumber, jobUrl, logUrl, GERRIT_PATCHSET_UPLOADER_NAME, currentBuild.currentResult)
                        error e.getMessage()
                    }
                }

                stage('package'){
                    try{
                        def status = base.scriptStatus(packageScript)
                        if(status != 0){
                            currentBuild.result = 'FAILURE'
                            base.influxDb()
                            error '[ERROR] 打包失败!'
                        }
                    }catch(e){
                        println '[ERROR] 打包失败!'
                        println e
                        currentBuild.result = 'FAILURE'
                        notification.wechatNotification(webhookUrl, jobName, buildNumber, jobUrl, logUrl, username, currentBuild.currentResult)
                        base.influxDb()
                        error e.getMessage()
                    }

                }

                stage('upload'){
                    print "artifact: ${artifact}"
                    print "deployTarget: ${deployTarget}"
                    try{
                        base.fileUpload(artifact, deployTarget)
                    }catch(e){
                        println "[ERROR] 上传失败!"
                        println e
                        currentBuild.result = 'FAILURE'
                        notification.wechatNotification(webhookUrl, jobName, buildNumber, jobUrl, logUrl, username, currentBuild.currentResult)
                        base.influxDb()
                        error e.getMessage()
                    }
                }

                stage('clean'){
                    def status = base.scriptStatus(cleanCmd)
                    if(status != 0){
                        println "[ERROR] 清理失败!"
                        notification.wechatNotification(webhookUrl, jobName, buildNumber, jobUrl, logUrl, username, currentBuild.currentResult)
                    }
                }

                stage('cherry-pick reset'){
                    dir(cherryPickDir){
                        base.cherryPickClean()
                    }
                }

                stage('influxDb'){
                    currentBuild.result = 'SUCCESS'
                    base.influxDb()
                }

                stage('retrun artifact'){
                    // notification.wechatNotification(webhookUrl, jobName, buildNumber, jobUrl, logUrl, username, currentBuild.currentResult)
                    currentBuild.description = """<span>存储地址:<a href=${storageURL}>${storageURL}</a></span>"""
                    return deployTarget
                }

            }
        }
}

def robotAutoBuildWithoutK8s(robot, arch, branch, distro, clean, version){

    def date = new Date()
    def robotBuildProfile = robotBuildProfile(robot, arch, branch, distro, version)
    def label = robotBuildProfile.label.get(arch).get(distro)
    def image = robotBuildProfile.image.get(arch).get(distro)
    def gitInfo = robotBuildProfile.gitUrl.get(arch)
    def artifactDir = robotBuildProfile.artifactDir
    def artifactName = robotBuildProfile.artifactName
    def artifact =  artifactDir + artifactName
    def buildScript = robotBuildProfile.buildScript
    def cleanScript = robotBuildProfile.cleanScript
    def packageScript = robotBuildProfile.packageScript
    def artifactory = robotBuildProfile.artifactory + '/build'
    def cleanCmd = robotBuildProfile.cleanCmd
    def base = new base()
    def notification = new notification()
    def jobName = JOB_NAME
    def buildNumber = BUILD_NUMBER
    def jobUrl = JOB_URL
    def logUrl = BUILD_URL + 'console'
    def username = ''
    def deployTarget = base.backupFolder(artifactory, robot, branch, date)
    def server = Artifactory.server 'jfrog-artifactory'
    def srverUrl = server.url
    def storageURL

    println '============================================== BUILD INFO =============================================='
    println robotBuildProfile
    println "label: ${label}"
    println "gitInfo: ${gitInfo}"
    println "artifactDir: ${artifactDir}"
    println "artifactName: ${artifactName}"
    println "artifact: ${artifact}"
    println "buildScript: ${buildScript}"
    println "packageScript: ${packageScript}"
    println "artifactory: ${artifactory}"
    println '========================================================================================================'

    node(label){

        stage('checkout'){
            storageURL = srverUrl + '/' + deployTarget
            try{
                base.gitRepos(gitInfo, branch)
            }catch(e){
                println '[ERROR] 代码克隆失败!'
                println e
                currentBuild.result = 'FAILURE'
                base.influxDb()
                error e.getMessage()
            }
        }
        stage('build'){
            if(clean){
                def status = base.scriptStatus(cleanScript)
            }
            try{
                def status = base.scriptStatus(buildScript)
                if(status != 0){
                    currentBuild.result = 'FAILURE'
                    base.influxDb()
                    error '[ERROR] 编译失败!'
                }
            }catch(e){
                println '[ERROR] 编译失败!'
                println e
                currentBuild.result = 'FAILURE'
                base.influxDb()
                error e.getMessage()
            }
        }
        stage('package'){
            try{
                def status = base.scriptStatus(packageScript)
                if(status != 0){
                    currentBuild.result = 'FAILURE'
                    base.influxDb()
                    error '[ERROR] 打包失败!'
                }
            }catch(e){
                println '[ERROR] 打包失败!'
                println e
                currentBuild.result = 'FAILURE'
                base.influxDb()
                error e.getMessage()
            }
        }
        stage('upload'){
            print "artifact: ${artifact}"
            print "deployTarget: ${deployTarget}"
            try{
                base.fileUpload(artifact, deployTarget)
            }catch(e){
                println "[ERROR] 上传失败!"
                println e
                currentBuild.result = 'FAILURE'
                base.influxDb()
                error e.getMessage()
            }
        }
        stage('clean'){
            def status = base.scriptStatus(cleanCmd)
            if(status != 0){
                println "[ERROR] 清理失败!"
            }
        }
        stage('influxDb'){
            currentBuild.result = 'SUCCESS'
            base.influxDb()
        }
        stage('retrun artifact'){
            currentBuild.description = """<span>存储地址:<a href=${storageURL}>${storageURL}</a></span>"""
            return deployTarget
        }
    }
}

def robotAutoBuildWithCalibration(robotName, arch, branch, distro, clean){

    def date = new Date()
    def builds = [:]
    def downloads = []
    def calibrationName = 'D200Calibration'
    def calibrationBuild = new calibrationBuild()
    def base = new base()
    def calibrationDeployProfile = calibrationDeployProfile(robotName, branch, BUILD_NUMBER)
    def packageName = calibrationDeployProfile.packageName
    def installName = calibrationDeployProfile.installName
    def calibration = calibrationDeployProfile.calibrationName
    def artifactory = calibrationDeployProfile.artifactory
    def localDir = './'

    def robotBuildProfile = robotBuildProfile(robotName, arch, branch, distro, BUILD_NUMBER)

    builds[robotName] = {
        def result = robotAutoBuild(robotName, arch, branch, distro, clean)
        downloads.add(result)
    }

    builds[calibrationName] = {
        def result = calibrationBuild.autoBuild(calibrationName, branch)
        downloads.add(result)
        sh "sleep 5m"
    }

    timestamps{
        parallel builds
        println downloads
        def server = Artifactory.server 'jfrog-artifactory'
        def downloadUrl
        def srverUrl = server.url

        node(){
            for(download in downloads){
                print download
                downloadUrl = download
                print downloadUrl
                base.fileDownload(downloadUrl, localDir)
            }

            // base.zipPackage(robotPackage, localPackage)
            sh """
                ls
                ls ${installName}
                ls ${calibration}
                tar zxvf ${installName}
                rm -f ${installName}
                tar zcvf ${packageName} .
            """

            def deployTarget = base.backupFolder(artifactory, robotName, branch, date)
            base.fileUpload(packageName, deployTarget)
            def zipStorage = srverUrl + '/' + deployTarget
            currentBuild.description = """<span>存储地址:<a href=${zipStorage}>${zipStorage}</a></span>"""

        }
    }

}

def robotAutoBuildForZ100(robot, arch, branch, distro, clean, release_name){

    def date = new Date()
    def robotBuildProfile = robotBuildProfile(robot, arch, branch, distro, release_name)
    def label = robotBuildProfile.label.get(arch).get(distro) + '_' + BUILD_NUMBER
    def image = robotBuildProfile.image.get(arch).get(distro)
    def volumes = robotBuildProfile.volumes.get(arch).get(distro)
    def gitInfo = robotBuildProfile.gitUrl.get(arch)
    def artifactDir = robotBuildProfile.artifactDir
    def artifactName = robotBuildProfile.artifactName
    def artifact =  artifactDir + artifactName
    def buildScript = robotBuildProfile.buildScript
    def cleanScript = robotBuildProfile.cleanScript
    def packageScript = robotBuildProfile.packageScript
    def artifactory = robotBuildProfile.artifactory + '/build'
    def slamDir = robotBuildProfile.slamDir
    def cleanCmd = robotBuildProfile.cleanCmd
    def base = new base()
    def notification = new notification()
    def jobName = JOB_NAME
    def buildNumber = BUILD_NUMBER
    def jobUrl = JOB_URL
    def logUrl = BUILD_URL + 'console'
    def username = ''
    def deployTarget = base.backupFolder(artifactory, robot, branch, date)
    def server = Artifactory.server 'jfrog-artifactory'
    def srverUrl = server.url
    def storageURL

    println '============================================== BUILD INFO =============================================='
    println robotBuildProfile
    println "label: ${label}"
    println "image: ${image}"
    println "volumes: ${volumes}"
    println "gitInfo: ${gitInfo}"
    println "artifactDir: ${artifactDir}"
    println "artifactName: ${artifactName}"
    println "artifact: ${artifact}"
    println "buildScript: ${buildScript}"
    println "packageScript: ${packageScript}"
    println "artifactory: ${artifactory}"
    println '========================================================================================================'

    podTemplate(
        cloud: 'kubernetes',
        containers: [
            containerTemplate(
                image: image,
                name: 'jnlp',
                ttyEnabled: true,
                alwaysPullImage: true,
                workingDir: '/home/jenkins/agent')
        ],
        label: label,
        name: 'yidao',
        namespace: 'kube-jenkins',
        volumes: volumes) {

            node(label){

                stage('checkout'){
                    storageURL = srverUrl + '/' + deployTarget
                    try{
                        base.gitRepos(gitInfo, branch)
                    }catch(e){
                        println '[ERROR] 代码克隆失败!'
                        println e
                        currentBuild.result = 'FAILURE'
                        base.influxDb()
                        error e.getMessage()
                    }
                }

                stage('build'){
                    if(clean){
                        def status = base.scriptStatus(cleanScript)
                    }
                    try{
                        def status = base.scriptStatus(buildScript)
                        if(status != 0){
                            currentBuild.result = 'FAILURE'
                            base.influxDb()
                            error '[ERROR] 编译失败!'
                        }
                    }catch(e){
                        println '[ERROR] 编译失败!'
                        println e
                        currentBuild.result = 'FAILURE'
                        base.influxDb()
                        error e.getMessage()
                    }
                }

                stage('package'){
                    base.downloadFile(slamDir, './')
                    sh "tar zxvf Z100V20slam.tar.gz install"
                    try{
                        def status = base.scriptStatus(packageScript)
                        if(status != 0){
                            currentBuild.result = 'FAILURE'
                            base.influxDb()
                            error '[ERROR] 打包失败!'
                        }
                    }catch(e){
                        println '[ERROR] 打包失败!'
                        println e
                        currentBuild.result = 'FAILURE'
                        base.influxDb()
                        error e.getMessage()
                    }

                }

                stage('upload'){
                    print "artifact: ${artifact}"
                    print "deployTarget: ${deployTarget}"
                    try{
                        base.fileUpload(artifact, deployTarget)
                    }catch(e){
                        println "[ERROR] 上传失败!"
                        println e
                        currentBuild.result = 'FAILURE'
                        base.influxDb()
                        error e.getMessage()
                    }
                }

                stage('clean'){
                    def status = base.scriptStatus(cleanCmd)
                    if(status != 0){
                        println "[ERROR] 清理失败!"
                    }
                }

                stage('influxDb'){
                    currentBuild.result = 'SUCCESS'
                    base.influxDb()
                }

                stage('retrun artifact'){
                    currentBuild.description = """<span>存储地址:<a href=${storageURL}>${storageURL}</a></span>"""
                    return deployTarget
                }

            }
        }
}


def robotAutoBuildForZ100Slam(robot, arch, branch, distro, clean){

    def robotBuildProfile = robotBuildProfile(robot, arch, branch, distro, BUILD_NUMBER)
    def label = robotBuildProfile.label.get(arch).get(distro) + '_' + BUILD_NUMBER
    def image = robotBuildProfile.image.get(arch).get(distro)
    def volumes = robotBuildProfile.volumes.get(arch).get(distro)
    def gitInfo = robotBuildProfile.gitUrl.get(arch)
    def artifactDir = robotBuildProfile.artifactDir
    def artifactName = robotBuildProfile.artifactName
    def artifact =  artifactDir + artifactName
    def buildScript = robotBuildProfile.buildScript
    def cleanScript = robotBuildProfile.cleanScript
    def packageScript = robotBuildProfile.packageScript
    def artifactory = robotBuildProfile.artifactory
    def cleanCmd = robotBuildProfile.cleanCmd
    def base = new base()
    def notification = new notification()
    def jobName = JOB_NAME
    def buildNumber = BUILD_NUMBER
    def jobUrl = JOB_URL
    def logUrl = BUILD_URL + 'console'
    def username = ''
    def deployTarget = artifactory
    def server = Artifactory.server 'jfrog-artifactory'
    def srverUrl = server.url
    def storageURL

    println '============================================== BUILD INFO =============================================='
    println robotBuildProfile
    println "label: ${label}"
    println "image: ${image}"
    println "volumes: ${volumes}"
    println "gitInfo: ${gitInfo}"
    println "artifactDir: ${artifactDir}"
    println "artifactName: ${artifactName}"
    println "artifact: ${artifact}"
    println "buildScript: ${buildScript}"
    println "packageScript: ${packageScript}"
    println "artifactory: ${artifactory}"
    println '========================================================================================================'

    podTemplate(
        cloud: 'kubernetes',
        containers: [
            containerTemplate(
                image: image,
                name: 'jnlp',
                ttyEnabled: true,
                alwaysPullImage: true,
                workingDir: '/home/jenkins/agent')
        ],
        label: label,
        name: 'yidao',
        namespace: 'kube-jenkins',
        volumes: volumes) {

            node(label){

                stage('checkout'){
                    storageURL = srverUrl + '/' + deployTarget
                    try{
                        base.gitRepos(gitInfo, branch)
                    }catch(e){
                        println '[ERROR] 代码克隆失败!'
                        println e
                        currentBuild.result = 'FAILURE'
                        base.influxDb()
                        error e.getMessage()
                    }
                }

                stage('build'){
                    if(clean){
                        def status = base.scriptStatus(cleanScript)
                    }
                    try{
                        def status = base.scriptStatus(buildScript)
                        if(status != 0){
                            currentBuild.result = 'FAILURE'
                            base.influxDb()
                            error '[ERROR] 编译失败!'
                        }
                    }catch(e){
                        println '[ERROR] 编译失败!'
                        println e
                        currentBuild.result = 'FAILURE'
                        base.influxDb()
                        error e.getMessage()
                    }
                }

                stage('package'){
                    def date = new Date().format("yyyyMMdd")
                    def time = new Date().format("HHmmss")
                    def commit_id
                    dir('src/Z100Slam'){
                        commit_id = base.scriptStdout('git rev-parse HEAD')
                    }
                    def ver = "build_date: ${date}_${time}\nbranch: ${branch}\ncommit_id: ${commit_id}"
                    base.writeFile(ver, 'install/slam_ver.txt')
                    sh "rm -rf install/local_setup*"
                    try{
                        def status = base.scriptStatus(packageScript)
                        if(status != 0){
                            currentBuild.result = 'FAILURE'
                            base.influxDb()
                            error '[ERROR] 打包失败!'
                        }
                    }catch(e){
                        println '[ERROR] 打包失败!'
                        println e
                        currentBuild.result = 'FAILURE'
                        base.influxDb()
                        error e.getMessage()
                    }

                }

                stage('upload'){
                    print "artifact: ${artifact}"
                    print "deployTarget: ${deployTarget}"
                    try{
                        base.fileUpload(artifact, deployTarget)
                    }catch(e){
                        println "[ERROR] 上传失败!"
                        println e
                        currentBuild.result = 'FAILURE'
                        base.influxDb()
                        error e.getMessage()
                    }
                }

                stage('clean'){
                    def status = base.scriptStatus(cleanCmd)
                    if(status != 0){
                        println "[ERROR] 清理失败!"
                    }
                }

                stage('influxDb'){
                    currentBuild.result = 'SUCCESS'
                    base.influxDb()
                }

                stage('retrun artifact'){
                    currentBuild.description = """<span>存储地址:<a href=${storageURL}>${storageURL}</a></span>"""
                    return deployTarget
                }

            }
        }
}

def robotAutoPreBuildForZ100(robot, arch, branch, distro, clean, release_name){

    def date = new Date()
    def robotBuildProfile = robotBuildProfile(robot, arch, branch, distro, release_name)
    def label = robotBuildProfile.label.get(arch).get(distro) + '_' + BUILD_NUMBER
    def image = robotBuildProfile.image.get(arch).get(distro)
    def volumes = robotBuildProfile.volumes.get(arch).get(distro)
    def gitInfo = robotBuildProfile.gitUrl.get(arch)
    def artifactDir = robotBuildProfile.artifactDir
    def artifactName = robotBuildProfile.artifactName
    def artifact =  artifactDir + artifactName
    def buildScript = robotBuildProfile.buildScript
    def cleanScript = robotBuildProfile.cleanScript
    def packageScript = robotBuildProfile.packageScript
    def artifactory = robotBuildProfile.artifactory + '/preBuild'
    def slamDir = robotBuildProfile.slamDir
    def cleanCmd = robotBuildProfile.cleanCmd
    def base = new base()
    def gitScript = new gitScript()
    def notification = new notification()
    def jobName = JOB_NAME
    def buildNumber = BUILD_NUMBER
    def jobUrl = JOB_URL
    def logUrl = BUILD_URL + 'console'
    def webhookUrl = robotBuildProfile.webhookUrl
    def username = 'changbin'
    def deployTarget = base.backupFolder(artifactory, robot, branch, date)
    def cherryPickDir = robotBuildProfile.cherryPickDir
    def cherryPickGit = robotBuildProfile.cherryPickGit
    def server = Artifactory.server 'jfrog-artifactory'
    def srverUrl = server.url
    def storageURL

    println '============================================== BUILD INFO =============================================='
    println robotBuildProfile
    println "label: ${label}"
    println "image: ${image}"
    println "volumes: ${volumes}"
    println "gitInfo: ${gitInfo}"
    println "artifactDir: ${artifactDir}"
    println "artifactName: ${artifactName}"
    println "artifact: ${artifact}"
    println "buildScript: ${buildScript}"
    println "packageScript: ${packageScript}"
    println "artifactory: ${artifactory}"
    println '========================================================================================================'

    podTemplate(
        cloud: 'kubernetes',
        containers: [
            containerTemplate(
                image: image,
                name: 'jnlp',
                ttyEnabled: true,
                alwaysPullImage: true,
                workingDir: '/home/jenkins/agent')
        ],
        label: label,
        name: 'yidao',
        namespace: 'kube-jenkins',
        volumes: volumes) {

            node(label){

                stage('checkout'){
                    storageURL = srverUrl + '/' + deployTarget
                    try{
                        base.gitRepos(gitInfo, branch)
                    }catch(e){
                        println '[ERROR] 代码克隆失败!'
                        println e
                        currentBuild.result = 'FAILURE'
                        base.influxDb()
                        notification.wechatNotification(webhookUrl, jobName, buildNumber, jobUrl, logUrl, GERRIT_PATCHSET_UPLOADER_NAME, currentBuild.currentResult)
                        error e.getMessage()
                    }
                }

                stage('cherry-pick'){
                    if(clean){
                        def status = base.scriptStatus(cleanScript)
                    }
                    dir(cherryPickDir){
                        try{
                            def status = base.cherryPick(cherryPickGit, GERRIT_REFSPEC)
                            if(status != 0){
                                currentBuild.result = 'FAILURE'
                                base.influxDb()
                                sh "git status"
                                error '[ERROR] cherry-pick 失败!请检查是否有冲突或者已 Merge'
                            }
                            gitScript.checkCommitMessage()
                        }catch(e){
                            println e
                            base.cherryPickFailClean()
                            currentBuild.result = 'FAILURE'
                            base.influxDb()
                            notification.commitTemplateNotification(webhookUrl, jobName, buildNumber, jobUrl, logUrl, GERRIT_PATCHSET_UPLOADER_NAME, e.getMessage())
                            error e.getMessage()
                        }
                    }
                }


                stage('build'){
                    try{
                        def status = base.scriptStatus(buildScript)
                        if(status != 0){
                            currentBuild.result = 'FAILURE'
                            base.influxDb()
                            error '[ERROR] 编译失败!'
                        }
                    }catch(e){
                        println '[ERROR] 编译失败!'
                        println e
                        currentBuild.result = 'FAILURE'
                        dir(cherryPickDir){
                            base.cherryPickClean()
                        }
                        base.influxDb()
                        notification.wechatNotification(webhookUrl, jobName, buildNumber, jobUrl, logUrl, GERRIT_PATCHSET_UPLOADER_NAME, currentBuild.currentResult)
                        error e.getMessage()
                    }
                }

                stage('package'){
                    base.downloadFile(slamDir, './')
                    sh "tar zxvf Z100V20slam.tar.gz install"
                    try{
                        def status = base.scriptStatus(packageScript)
                        if(status != 0){
                            currentBuild.result = 'FAILURE'
                            base.influxDb()
                            error '[ERROR] 打包失败!'
                        }
                    }catch(e){
                        println '[ERROR] 打包失败!'
                        println e
                        currentBuild.result = 'FAILURE'
                        notification.wechatNotification(webhookUrl, jobName, buildNumber, jobUrl, logUrl, username, currentBuild.currentResult)
                        base.influxDb()
                        error e.getMessage()
                    }

                }

                stage('upload'){
                    print "artifact: ${artifact}"
                    print "deployTarget: ${deployTarget}"
                    try{
                        base.fileUpload(artifact, deployTarget)
                    }catch(e){
                        println "[ERROR] 上传失败!"
                        println e
                        currentBuild.result = 'FAILURE'
                        notification.wechatNotification(webhookUrl, jobName, buildNumber, jobUrl, logUrl, username, currentBuild.currentResult)
                        base.influxDb()
                        error e.getMessage()
                    }
                }

                stage('clean'){
                    def status = base.scriptStatus(cleanCmd)
                    if(status != 0){
                        println "[ERROR] 清理失败!"
                        notification.wechatNotification(webhookUrl, jobName, buildNumber, jobUrl, logUrl, username, currentBuild.currentResult)
                    }
                }

                stage('cherry-pick reset'){
                    dir(cherryPickDir){
                        base.cherryPickClean()
                    }
                }

                stage('influxDb'){
                    currentBuild.result = 'SUCCESS'
                    base.influxDb()
                }

                stage('retrun artifact'){
                    // notification.wechatNotification(webhookUrl, jobName, buildNumber, jobUrl, logUrl, username, currentBuild.currentResult)
                    currentBuild.description = """<span>存储地址:<a href=${storageURL}>${storageURL}</a></span>"""
                    return deployTarget
                }

            }
        }
}


def robotAutoBuild_gitlab_http(robot, arch, branch, distro, clean, version){

    def date = new Date()
    def buildVersion
    if(robot == 'D200'){
        buildVersion = version + '.' + date.format('yyyyMMddHHmm') + '_' + BUILD_NUMBER
    }else{
        buildVersion = version
    }
    def robotBuildProfile = robotBuildProfile(robot, arch, branch, distro, buildVersion)
    def label = robotBuildProfile.label.get(arch).get(distro) + '_' + BUILD_NUMBER
    def image = robotBuildProfile.image.get(arch).get(distro)
    def volumes = robotBuildProfile.volumes.get(arch).get(distro)
    def gitInfo = robotBuildProfile.gitUrl.get(arch)
    def artifactDir = robotBuildProfile.artifactDir
    def artifactName = robotBuildProfile.artifactName
    def artifact =  artifactDir + artifactName
    def buildScript = robotBuildProfile.buildScript
    def cleanScript = robotBuildProfile.cleanScript
    def packageScript = robotBuildProfile.packageScript
    def artifactory = robotBuildProfile.artifactory + '/build'
    def cleanCmd = robotBuildProfile.cleanCmd
    def webhookUrl = robotBuildProfile.webhookUrl
    def base = new base()
    def notification = new notification()
    def jobName = JOB_NAME
    def buildNumber = BUILD_NUMBER
    def jobUrl = JOB_URL
    def logUrl = BUILD_URL + 'console'
    def username = ''
    def deployTarget = base.backupFolder(artifactory, robot, branch, date)
    def server = Artifactory.server 'jfrog-artifactory'
    def srverUrl = server.url
    def storageURL

    println '============================================== BUILD INFO =============================================='
    println robotBuildProfile
    println "label: ${label}"
    println "image: ${image}"
    println "volumes: ${volumes}"
    println "gitInfo: ${gitInfo}"
    println "artifactDir: ${artifactDir}"
    println "artifactName: ${artifactName}"
    println "artifact: ${artifact}"
    println "buildScript: ${buildScript}"
    println "packageScript: ${packageScript}"
    println "artifactory: ${artifactory}"
    println '========================================================================================================'

    podTemplate(
        cloud: 'kubernetes',
        containers: [
            containerTemplate(
                image: image,
                name: 'jnlp',
                ttyEnabled: true,
                alwaysPullImage: true,
                workingDir: '/home/jenkins/agent')
        ],
        label: label,
        name: 'yidao',
        namespace: 'kube-jenkins',
        volumes: volumes) {

            node(label){

                stage('checkout'){
                    storageURL = srverUrl + '/' + deployTarget
                    try{

                        base.gitlabRepos(gitInfo, branch)
                    }catch(e){
                        println '[ERROR] 代码克隆失败!'
                        println e
                        currentBuild.result = 'FAILURE'
                        base.influxDb()
                        error e.getMessage()
                    }
                }

                stage('build'){
                    if(clean){
                        def status = base.scriptStatus(cleanScript)
                    }
                    try{
                        def status = base.scriptStatus(buildScript)
                        if(status != 0){
                            currentBuild.result = 'FAILURE'
                            base.influxDb()
                            error '[ERROR] 编译失败!'
                        }
                    }catch(e){
                        println '[ERROR] 编译失败!'
                        println e
                        currentBuild.result = 'FAILURE'
                        base.influxDb()
                        notification.wechatNotification(webhookUrl, jobName, buildNumber, jobUrl, logUrl, '', currentBuild.currentResult)
                        error e.getMessage()
                    }
                }

                stage('package'){
                    try{
                        def status = base.scriptStatus(packageScript)
                        if(status != 0){
                            currentBuild.result = 'FAILURE'
                            base.influxDb()
                            error '[ERROR] 打包失败!'
                        }
                    }catch(e){
                        println '[ERROR] 打包失败!'
                        println e
                        currentBuild.result = 'FAILURE'
                        base.influxDb()
                        error e.getMessage()
                    }

                }

                stage('upload'){
                    print "artifact: ${artifact}"
                    print "deployTarget: ${deployTarget}"
                    try{
                        base.fileUpload(artifact, deployTarget)
                    }catch(e){
                        println "[ERROR] 上传失败!"
                        println e
                        currentBuild.result = 'FAILURE'
                        base.influxDb()
                        error e.getMessage()
                    }
                }

                stage('clean'){
                    def status = base.scriptStatus(cleanCmd)
                    if(status != 0){
                        println "[ERROR] 清理失败!"
                    }
                }

                stage('influxDb'){
                    currentBuild.result = 'SUCCESS'
                    base.influxDb()
                }

                stage('retrun artifact'){
                    currentBuild.description = """<span>存储地址:<a href=${storageURL}>${storageURL}</a></span>"""
                    return deployTarget
                }

            }
        }
}
